<template>
    <div>
        <page-header  @toLastPage="toLastPage">
            <template v-slot:title >
                <span >
                    资料设置
                </span>
            </template>
        </page-header>
       <div class="inf-wrapper">
             <!-- 功能列表 -->
        <div class="list" v-for="(item,index) in userInfList" :key="index">
            <cell-list :itemList="item" @sendEvent="sendEvent" @switchChange='switchChange'></cell-list>
        </div>
        <van-button color="#fff" block @click="deleteFriends">删除</van-button>
       </div>
    </div>
</template>
<script>
import pageHeader from '../common/pageHeader';
import cellList from '../common/cellList';
import {Meteor} from 'meteor/meteor';
import { user,friends } from '../../../api/collections/index';
export default {
    components:{
        pageHeader,
        cellList
    },
     meteor: {
    $subscribe: {
      'userInf': []
    },
    userInf(){
        const inf = friends.findOne({F_UserID:this.id,F_FriendID:this.friendId})
        const userInf = user.findOne({_id:this.friendId})
        if(inf && userInf){
            this.userInfList[0].moreList[0].content = inf.F_beizhu?inf.F_beizhu:userInf.nickname
            this.userInfList[3].checked = inf.F_isBlock
        }
    }
    },
    data() {
        return {
            userInfList:[ {
        separate:false,
         moreList:[
           {
            describe:'设置备注与标签',
            icon:'',
            content:'',
            color:'',
            event:'setRemarks'
           },
           {
              describe:'朋友权限',
              icon:'',
              content:'',
              color:''
           }
         ]
        },
        {
        separate:false,
         moreList:[
           {
            describe:'把他推荐给朋友',
            icon:'',
            content:'',
            color:'',
            event:''
           },
           {
              describe:'添加到桌面',
              icon:'',
              content:'',
              color:''
           }
         ]
        },
        {
            separate:true,
             describe:'设为星标好友',
              icon:'',
              content:'',
              switch:true,
              checked:false,
              event:'no-ops',
              switch_event:'setStart'
        },
        {
            separate:true,
             describe:'加入黑名单',
              icon:'',
              content:'',
              switch:true,
              checked:false,
              event:'no-ops',
              switch_event:'putInBlock'
        },
         {
             separate:true,
            describe:'投诉',
            icon:'',
            content:'',
            color:'',
            event:''
           },
        ],
            friendId:this.$route.params.id,
            id:sessionStorage.getItem('user')
        }
    },
    methods:{
        toLastPage(){
            this.$router.go(-1)
        },
        sendEvent(e){
            switch(e){
                case 'no-ops':break;
                case 'setRemarks':this.$router.push('/setRemarks/'+this.friendId);break;
                default:this.$notify({ type: 'success', message: '功能暂未开发',duration: 500 });break;
            }
        },
        switchChange(e){
            switch(e.switch_event){
                case 'setStart':this.$notify({ type: 'success', message: '功能暂未开发',duration: 500 });break;
                case 'putInBlock':Meteor.call('friends.setBlock',{
                    uid:this.id,
                    fid:this.friendId,
                    isBlock:e.checked
                })
            }
        },
        deleteFriends(){
            this.$dialog.confirm({
            title: '删除联系人',
            message: '确定要删除该联系人吗?删除后您将无法再对其发起会话',
            })
            .then(() => {
                Meteor.call('friends.delete',{
                     uid:this.id,
                    fid:this.friendId
                },(err,res)=>{
                    if(err){
                        this.$toast(err.error)
                    }
                    else{
                        Meteor.call('indexMsg.delete',{
                            userId:this.id,
                            otherId:this.friendId
                        },(err,res)=>{
                             if(err){
                        this.$toast(err.error)
                    }else{
                        this.$toast.success('删除成功')
                        this.$router.push('/home')
                    }
                        })
                    }
                })
            })
            .catch(() => {
                // on cancel
            });
                    }
                }
}
</script>
<style scoped>
.inf-wrapper{
    margin-top: 1.5rem;
}
.van-button{
    color: #ff0000!important;
    font-weight: 700;
    margin-bottom: 1rem;
}
</style>